package com.solution.admin.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductCategorySpecEntity;
import com.solution.admin.product.service.ProductCategorySpecService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品类型关联规格web控制层
 * @author solin
 * @date 2017-10-11 17:38:04
 */
@RequestMapping("/productCategorySpec")
@Controller
public class ProductCategorySpecController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductCategorySpecController.class);

	@Autowired
	private ProductCategorySpecService productCategorySpecService;
	
	/**
	 * 跳转到选择规格页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年10月11日 下午3:16:24
	 */
	@RequestMapping("/toProductSpecSelector")
	public String toProductSpecSelector(HttpServletRequest request) {
		return "product/productSpec_selector";
	}
	
	/**
	 * 跳转商品类型关联规格查询页面
	 * @param ProductCategorySpec 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-10-11 17:38:04
	 */
	@RequestMapping("/toListProductCategorySpecPage")
	public String toListProductCategorySpecPage(ProductCategorySpecEntity productCategorySpec, HttpServletRequest request) {
		return "product/productCategorySpec_list";
	}

	/**
	 * 分页查询商品类型关联规格列表
	 * @param productCategorySpec 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-10-11 17:38:04
	 */
	@RequestMapping("/listProductCategorySpecPage")
	@ResponseBody
	public JSONGrid listProductCategorySpecPage(ProductCategorySpecEntity productCategorySpec, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("productCategoryId", productCategorySpec.getProductCategoryId());
		try {
			PageBean pageBean = productCategorySpecService.listProductCategorySpecPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品类型关联规格失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-10-11 17:38:04
	 */
	@RequestMapping("/toAddProductCategorySpec")
	public String toAddProductCategorySpec(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productCategorySpec_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-10-11 17:38:04
	 */
	@RequestMapping("/toUpdateProductCategorySpec")
	public String toUpdateProductCategorySpec(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductCategorySpecEntity productCategorySpec = productCategorySpecService.getProductCategorySpecById(id);
			request.setAttribute("productCategorySpec", productCategorySpec);
		} catch (Exception e) {
			handleException("跳转编辑商品类型关联规格页面错误", logger, e);
		}
		return "product/productCategorySpec_form";
	}


	/**
	 * 新增或保存商品类型关联规格
	 * @param request 请求对象
	 * @param productCategorySpecEntity 商品类型关联规格实体
	 * @return
	 * @author solin
 	 * @date 2017-10-11 17:38:04
	 */
	@RequestMapping("/saveOrUpdateProductCategorySpec")
	@ResponseBody
	public Map<String, Object> saveProductCategorySpec(HttpServletRequest request, ProductCategorySpecEntity productCategorySpecEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = productCategorySpecService.updateProductCategorySpec(productCategorySpecEntity);
			}else{
				productCategorySpecEntity.setCreater(this.getUserName());
				result = (int)productCategorySpecService.saveProductCategorySpec(productCategorySpecEntity);
				resultMap.put("id", result);
			}
		} catch (Exception e) {
			handleException("添加商品类型关联规格失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}
	
	/**
	 * 删除商品类型关联规格
	 * @param request
	 * @param productCategorySpecEntity
	 * @return
	 * @author: solin
	 * @date: 2017年10月11日 下午8:36:00
	 */
	@RequestMapping("/removeProductCategorySpec")
	@ResponseBody
	public Map<String, Object> removeProductCategorySpec(HttpServletRequest request, ProductCategorySpecEntity productCategorySpecEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			result = productCategorySpecService.deleteProductCategorySpecById(productCategorySpecEntity.getId());
		} catch (Exception e) {
			handleException("添加商品类型关联规格失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}
	
}
